<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>剑指 Offer 10- I. 斐波那契数列</title>
  </head>
  <body>
    <script>
      //-------------方法一------------
      //超时了
      // var fib = function (n) {
      //   var num = 0;
      //   if (n == 0) {
      //     return 0;
      //   } else if (n == 1) {
      //     return 1;
      //   } else {
      //     return (num = fib(n - 1) + fib(n - 2));
      //   }
      // };
      //-----------------------
      //------------方法二----------------
      var fib = function (n) {
        let a = 0,
          b = 1,
          num = 0;
        if (n === 1) return 1;
        for (let i = 0; i < n - 1; i++) {
          num = (a + b) % 1000000007;
          a = b;
          b = num;
        }
        return num;
      };
      var n = 2;
      console.log(fib(50));
    </script>
  </body>
</html>
